﻿using System.Reflection;


[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
namespace La.Code.Log
{

    public class LogEntity2
    {
        /// <summary>
        /// 
        /// </summary>
        public string UserId { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string UserName { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public object Message { get; set; }
    }
    /// <summary>
    /// 布局
    /// </summary>
    public class MyLayout : log4net.Layout.PatternLayout
    {
        public MyLayout()
        {
            AddConverter("Property", typeof(MyPatternConverter));
        }

    }
    public class MyPatternConverter : log4net.Layout.Pattern.PatternLayoutConverter
    {
        protected override void Convert(System.IO.TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
        {
            if (Option != null)
            {
                WriteObject(writer, loggingEvent.Repository, LookUp(Option, loggingEvent));
            }
            else
            {
                WriteObject(writer, loggingEvent.Repository, loggingEvent);
            }

        }


        private object LookUp(string property, log4net.Core.LoggingEvent loggingEvent)
        {
            object propertyValue = string.Empty;
            PropertyInfo propertyInfo = loggingEvent.MessageObject.GetType().GetProperty(property);
            if (propertyInfo != null)
                propertyValue = propertyInfo.GetValue(loggingEvent.MessageObject, null);
            return propertyValue;
        }
    }


}